package com.longteng.java.web.weather.impl;

import com.longteng.java.web.weather.CityFinder;
import com.longteng.java.web.weather.FindCityRequest;
import com.longteng.java.web.weather.Response;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.net.URI;

/**
 * Created by tianqing.wang
 * DATE : 2018/4/15
 * TIME : 下午5:37
 * PROJECT : web-lesson
 * PACKAGE : com.longteng.java.web.weather.impl
 *
 * @author <a href="mailto:celeskyking@163.com">tianqing.wang</a>
 */
@Service
public class CityFinderImpl implements CityFinder{

    @Resource
    private RestTemplate restTemplate;

    @Override
    public Response find(FindCityRequest request) {
        URI uri = UriComponentsBuilder.newInstance().scheme("https")
                .host("search.heweather.com")
                .path("/find")
                .queryParam("key","5c9e1aec3f9440608e673fd4705bf596")
                .queryParam("location",request.getLocation())
                .build().encode().toUri();
        return restTemplate.getForObject(uri,Response.class);
    }
}
